1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
System;
5 using
System.IO;
6 using
System.Runtime.Serialization.Formatters.Binary;
7
8 public
class GameController : MonoBehaviour {
9     
public static GameController instance;
10
11     
public bool isGameStartedFirstTime;
12     
public bool isMusicOn;
13
14     
public int highScore;
15     
public int currentScore;
16
17     
private GameData data;
18
19     
void Awake(){
20         CreateInstance ();
21         InitializeGameVariables ();
22     }
23
24     
void CreateInstance(){
25         
if (instance != null) {
26             Destroy (gameObject);
27         }
else {
28             instance =
this;
29             DontDestroyOnLoad (gameObject);
30         }
31     }
32
33     
void InitializeGameVariables(){
34         Load ();
35
36         
if (data != null) {
37             isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
38         }
else {
39             isGameStartedFirstTime =
true;
40         }
41
42         
if (isGameStartedFirstTime) {
43             isGameStartedFirstTime =
false;
44             isMusicOn =
true;
45             highScore =
0;
46             data =
new GameData ();
47             data.SetIsGameStartedFirstTime (isGameStartedFirstTime);
48             data.SetHighScore (highScore);
49             data.SetIsMusicOn (isMusicOn);
50
51             Save ();
52
53             Load ();
54         }
else {
55             isMusicOn = data.GetIsMusicOn ();
56             isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
57             highScore = data.GetHighScore ();
58         }
59
60     }
61
62     
public void Save(){
63         FileStream file =
null;
64         
try{
65             BinaryFormatter bf =
new BinaryFormatter();
66             file = File.Create(Application.persistentDataPath +
"/data.dat");
67             
if(data != null){
68                 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
69                 data.SetIsMusicOn(isMusicOn);
70                 data.SetHighScore(highScore);
71                 bf.Serialize(file, data);
72             }
73         }
catch(Exception e){
74             Debug.LogException (e,
this);
75         }
finally{
76             
if(file != null){
77                 file.Close ();
78             }
79         }
80     }
81
82     
public void Load(){
83         FileStream file =
null;
84         
try{
85             BinaryFormatter bf =
new BinaryFormatter();
86             file = File.Open(Application.persistentDataPath +
"/data.dat", FileMode.Open);
87             data = bf.Deserialize(file)
as GameData;
88         }
catch(Exception e){
89             Debug.LogException (e,
this);
90         }
finally{
91             
if(file != null){
92                 file.Close ();
93             }
94         }
95     }
96
97 }

98
99 [Serializable]

100 class
GameData{
101     
private bool isGameStartedFirstTime;
102     
private bool isMusicOn;
103     
private int highScore;
104
105     
public void SetIsGameStartedFirstTime(bool isGameStartedFirstTime){
106         
this.isGameStartedFirstTime = isGameStartedFirstTime;
107     }
108
109     
public bool GetIsGameStartedFirstTime(){
110         
return this.isGameStartedFirstTime;
111     }
112
113     
public void SetIsMusicOn(bool isMusicOn){
114         
this.isMusicOn = isMusicOn;
115     }
116
117     
public bool GetIsMusicOn(){
118         
return this.isMusicOn;
119     }
120
121     
public void SetHighScore(int highScore){
122         
this.highScore = highScore;
123     }
124
125     
public int GetHighScore(){
126         
return this.highScore;
127     }
128 }


Gõ tìm kiếm nhanh...